/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.rec.bc;

import edu.upn.sigemfi.rec.be.CastigoCredito;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Diego
 */
public class CastigoCreditoDelegate {
        private CastigoCreditoFacadeLocal local;

    public CastigoCreditoDelegate(){
        local = lookupCastigoCreditoFacade();
    }

    private CastigoCreditoFacadeLocal lookupCastigoCreditoFacade() {
        try {
            Context c = new InitialContext();
            return (CastigoCreditoFacadeLocal) c.lookup("java:comp/env/CastigoCreditoFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    

    public void crear(CastigoCredito castigoCredito) throws Exception
    {
        try{
            local.create(castigoCredito);
        }
        catch(Exception ex){
            local=lookupCastigoCreditoFacade();
            throw new Exception("Crear CastigoCredito"+ex.getMessage());
        }
    }

    public void editar(CastigoCredito castigoCredito) throws Exception
    {
        try{
            local.edit(castigoCredito);
        }catch (Exception ex){
            local=lookupCastigoCreditoFacade();
            throw new Exception("Editar CastigoCredito"+ex.getMessage());
        }
    }

    public void eliminar(CastigoCredito castigoCredito) throws Exception
    {
        try{
            local.remove(castigoCredito);
        }
        catch(Exception ex){
            local=lookupCastigoCreditoFacade();
            throw new Exception("Crear CastigoCredito"+ex.getMessage());
        }
    }

    public List<CastigoCredito> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupCastigoCreditoFacade();
            throw new Exception("Listar CastigoCreditos. " + e.getMessage(), e);
        }
    }

    public CastigoCredito buscar(Long codigo) throws Exception {
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local = lookupCastigoCreditoFacade();
            throw new Exception("Buscar CastigoCredito. " + e.getMessage(), e);
        }
    }

}
